import QtQuick
import QtQuick.Controls

Item{
    id: miniProcessList
    width: 610
    height: 440
    // color: "red"

    property int selectedIndex: -1  // 跟踪当前选中项索引
    property real showFontSize: 20
    property real recHeight: 40
    property real recWidth: 55


    // 写入参数合集
    property string write_Process1
    property string write_Process2
    property string write_Fu_AE_knife_1
    property string write_Fu_AE_knife_2
    property string write_Bu_AE_knife_1
    property string write_Bu_AE_knife_2
    property string write_Fu_knife_1
    property string write_Fu_knife_2
    property string write_Bu_knife_1
    property string write_Bu_knife_2
    property string write_Fl_knife_1
    property string write_Fl_knife_2
    property string write_Bl_knife_1
    property string write_Bl_knife_2
    property string write_Roller



    Component.onCompleted: {
        processManager.getAllData();
    }

    Rectangle{
        anchors.fill: parent
        color: "transparent"
        clip: true
        // border.width: 1

        Row{
            z:10
            Rectangle{
                border.width: 1 ;width: 70 ;height: recHeight;
                Text{anchors.centerIn: parent ; font.pixelSize: showFontSize ;
                    text: qsTr("序号")
                }
            }
            Rectangle{
                border.width: 1 ;width: 300 ;height: recHeight ;
                Text{anchors.centerIn: parent ;font.pixelSize: showFontSize ;
                    text: qsTr("名  称")
                }
            }
        }

        ListView{
            id: materialListView
            anchors.top: parent.top
            anchors.topMargin: 40
            anchors.left: parent.left
            width: 370
            height: 400
            model:processModel
            boundsBehavior: Flickable.StopAtBounds
            // 监听数据库数据加载完成
            Connections {
                target: processManager
                function onDataLoaded(model) {
                    processModel.copyModel(model);
                    // showStatus("数据已同步");
                }
            }

            // 监听模型数据变化
            Connections {
                target: processModel
                function onModelDataUpdated() {
                    materialListView.currentIndex = -1;
                    selectedIndex = -1;
                    // editPopup.close();
                }
            }

            // 仅跟踪索引，不直接操作currentItem
            onCurrentIndexChanged: {
                selectedIndex = currentIndex;
            }

            delegate: Item {
                width: 370
                height: recHeight

                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        materialListView.currentIndex = index;
                    }
                }

                Row {
                    id: dataRow
                    height: parent.height

                    // 序号列
                    Rectangle {
                        width: 70
                        height: parent.height
                        color: materialListView.currentIndex === index ? "gold" : "white"
                        border.color: "#eeeeee"
                        Text {
                            anchors.centerIn: parent
                            text: (index + 1)
                            elide: Text.ElideRight
                            horizontalAlignment: Text.AlignHCenter
                        }
                    }

                    // 名称列
                    Rectangle {
                        width: 300
                        height: parent.height
                        color: materialListView.currentIndex === index ? "gold" : "white"
                        border.color: "#eeeeee"
                        Text {
                            anchors.centerIn: parent
                            text: Name || qsTr("未命名")
                            font.pixelSize: showFontSize
                            elide: Text.ElideRight
                            width: parent.width - 8
                            horizontalAlignment: Text.AlignHCenter
                        }
                    }

                }
            }
            ScrollBar.vertical: ScrollBar {
                policy: ScrollBar.AlwaysOn
                width: 10
                anchors.right: parent.right
            }
        }

    }


    Column{
        anchors.top: miniProcessList.top
        // anchors.topMargin: 30
        anchors.right: miniProcessList.right
        anchors.rightMargin: 10
        spacing: 10

        A_Button{
            contentText_Data: qsTr("打  印")
            buttonEnable: selectedIndex !== -1 && selectedIndex < processModel.count()
            onButtonClicked: {
                console.log(selectedIndex)
                // materialListView.index = -1
                // selectedIndex = -1;
                const selectedItem = processModel.get(selectedIndex);
                if (selectedItem) {
                    Cpp_Printer.print_Active_WeiHuang(selectedItem.Name,
                                                      selectedItem.Process1,
                                                      selectedItem.Process2,
                                                      selectedItem.F_UpperKnife01Assist,
                                                      selectedItem.F_UpperKnife02Assist,
                                                      selectedItem.B_UpperKnife01Assist,
                                                      selectedItem.B_UpperKnife02Assist,
                                                      selectedItem.F_UpperKnife01,
                                                      selectedItem.F_UpperKnife02,
                                                      selectedItem.B_UpperKnife01,
                                                      selectedItem.B_UpperKnife02,
                                                      selectedItem.F_LowerKnife01,
                                                      selectedItem.F_LowerKnife02,
                                                      selectedItem.B_LowerKnife01,
                                                      selectedItem.B_LowerKnife02,
                                                      selectedItem.Roller);

                }
            }
        }


        A_Button{
            contentText_Data: qsTr("下载参数")
            buttonEnable: selectedIndex !== -1 && selectedIndex < processModel.count()
            onButtonClicked: {
                if (selectedIndex !== -1 && selectedIndex < processModel.count()) {
                    // 获取当前选中项数据
                    const selectedItem = processModel.get(selectedIndex);
                    if (selectedItem) {
                        // 输出所选行的参数到控制台
                        // console.log("当前所选行参数：");
                        // console.log(`工进1: ${selectedItem.Process1 || ""}`);
                        // console.log(`工进2: ${selectedItem.Process2 || ""}`);
                        // console.log(`前1辅: ${selectedItem.F_UpperKnife01Assist || ""}`);
                        // console.log(`前2辅: ${selectedItem.F_UpperKnife02Assist || ""}`);
                        // console.log(`后1辅: ${selectedItem.B_UpperKnife01Assist || ""}`);
                        // console.log(`后2辅: ${selectedItem.B_UpperKnife02Assist || ""}`);
                        // console.log(`前上1: ${selectedItem.F_UpperKnife01 || ""}`);
                        // console.log(`前上2: ${selectedItem.F_UpperKnife02 || ""}`);
                        // console.log(`后上1: ${selectedItem.B_UpperKnife01 || ""}`);
                        // console.log(`后上2: ${selectedItem.B_UpperKnife02 || ""}`);
                        // console.log(`前下1: ${selectedItem.F_LowerKnife01 || ""}`);
                        // console.log(`前下2: ${selectedItem.F_LowerKnife02 || ""}`);
                        // console.log(`后下1: ${selectedItem.B_LowerKnife01 || ""}`);
                        // console.log(`后下2: ${selectedItem.B_LowerKnife02 || ""}`);
                        // console.log(`压辊: ${selectedItem.Roller || ""}`);

                        write_Process1 = selectedItem.Process1 === "" ? "0" : selectedItem.Process1
                        write_Process2 = selectedItem.Process2 === "" ? "0" : selectedItem.Process2
                        write_Fu_AE_knife_1 = selectedItem.F_UpperKnife01Assist === "" ? "0" : selectedItem.F_UpperKnife01Assist
                        write_Fu_AE_knife_2 = selectedItem.F_UpperKnife02Assist === "" ? "0" : selectedItem.F_UpperKnife02Assist
                        write_Bu_AE_knife_1 = selectedItem.B_UpperKnife01Assist === "" ? "0" : selectedItem.B_UpperKnife01Assist
                        write_Bu_AE_knife_2 = selectedItem.B_UpperKnife02Assist === "" ? "0" : selectedItem.B_UpperKnife02Assist
                        write_Fu_knife_1 = selectedItem.F_UpperKnife01 === "" ? "0" : selectedItem.F_UpperKnife01
                        write_Fu_knife_2 = selectedItem.F_UpperKnife02 === "" ? "0" : selectedItem.F_UpperKnife02
                        write_Bu_knife_1 = selectedItem.B_UpperKnife01 === "" ? "0" : selectedItem.B_UpperKnife01
                        write_Bu_knife_2 = selectedItem.B_UpperKnife02 === "" ? "0" : selectedItem.B_UpperKnife02
                        write_Fl_knife_1 = selectedItem.F_LowerKnife01 === "" ? "0" : selectedItem.F_LowerKnife01
                        write_Fl_knife_2 = selectedItem.F_LowerKnife02 === "" ? "0" : selectedItem.F_LowerKnife02
                        write_Bl_knife_1 = selectedItem.B_LowerKnife01 === "" ? "0" : selectedItem.B_LowerKnife01
                        write_Bl_knife_2 = selectedItem.B_LowerKnife02 === "" ? "0" : selectedItem.B_LowerKnife02
                        write_Roller = selectedItem.Roller === "" ? "0" : selectedItem.Roller

                        Cpp_PLC.write_into_tcp_Hold_15_Parameters(write_Process1,
                                                                  write_Process2,
                                                                  write_Fu_AE_knife_1,
                                                                  write_Fu_AE_knife_2,
                                                                  write_Bu_AE_knife_1,
                                                                  write_Bu_AE_knife_2,
                                                                  write_Fu_knife_1,
                                                                  write_Fu_knife_2,
                                                                  write_Bu_knife_1,
                                                                  write_Bu_knife_2,
                                                                  write_Fl_knife_1,
                                                                  write_Fl_knife_2,
                                                                  write_Bl_knife_1,
                                                                  write_Bl_knife_2,
                                                                  write_Roller)


                    } else {
                        console.log("无法获取选中项数据");
                    }
                } else {
                    console.log("未选中任何行");
                }
            }
        }

        // 搜索输入框
        Rectangle {
            width: 200
            height: 50
            border.width: 1
            border.color: "#cccccc"

            TextField {
                id: searchInput
                anchors.fill: parent
                padding: 8
                font.pixelSize: 24
                placeholderText: qsTr("输入关键词搜索...")
                onAccepted: {
                    // 按回车键也能搜索
                    processManager.searchData(searchInput.text);
                    focus = false
                }
                onTextChanged: {
                    // 清空输入时自动显示全部数据
                    if (text.trim() === "") {
                        processManager.searchData(searchInput.text);
                        // processManager.getAllData();
                    }
                }
            }
        }

        // 搜索按钮
        Rectangle {
            width: 100
            height: 50
            color: "#2196F3"
            border.width: 1
            border.color: "#1976D2"

            Text {
                anchors.centerIn: parent
                color: "white"
                font.pixelSize: 24
                text: qsTr("搜索")
            }

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    parent.forceActiveFocus()
                    processManager.searchData(searchInput.text);
                }
            }
        }

        // 重置按钮
        Rectangle {
            width: 100
            height: 50
            color: "#f44336"
            border.width: 1
            border.color: "#d32f2f"

            Text {
                anchors.centerIn: parent
                color: "white"
                font.pixelSize: 24
                text: qsTr("重置")
            }

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    searchInput.text = "";
                    // searchKeyword = "";
                    parent.forceActiveFocus()
                    processManager.searchData(searchInput.text);
                    // processManager.getAllData(); // 显示全部数据
                }
            }
        }
    }











}
